<?php
/*
define('DBSERV', 'localhost');
define('DBUSER', 'root');
define('DBPASSWD', '');
define('DB', 'learner_profile');
*/

define('DBSERV', 'localhost');
define('DBUSER', 'vsbindus_devlp');
define('DBPASSWD', 'learnp)))');
define('DB', 'vsbindus_dev1');


define('APIKEY', 'd6da2a5570f2dee467be31ced7339e3e');

function isValidMd5($md5)
{
    return !empty($md5) && preg_match('/^[a-f0-9]{32}$/', $md5);
}

/* recursively retrieve records in tables */
function fetchRows( $tbs, $field, $value )
{
	$arr = array();
	if( sizeof($tbs)==1 )
	{
		$tb = array_pop( $tbs );
		
		$query = "select * from $tb where $field = $value";
		
		$result = mysql_query( $query ) or die( 'Query_failed: ' . mysql_error() );
		
		$arr[$tb.'s'] = array();
				
		while( $row = mysql_fetch_assoc( $result ) )
	    {
			array_push($arr[$tb.'s'], $row);
		}
		
		//echo ( json_encode($arr));
		return $arr;
	}
	else{
		$tb = array_pop( $tbs );
		$query = "select * from $tb where $field = $value";
		$result = mysql_query( $query ) or die( 'Query_failed: ' . mysql_error() );
		
		$arr[$tb.'s'] = array();
		
		while( $row = mysql_fetch_assoc( $result ) )
	    {
			array_push( $arr[$tb.'s'], array_merge( $row, fetchRows( $tbs, $tb.'id', $row['id'])) );
		}
				
		return $arr;
	}
}

/*update tables recursively*/
function updateTables( $tbs, $field, $value, $data )
{
	$tb = array_pop( $tbs );
	$dts = $data[$tb.'s'];
	
	if( sizeof($tbs)==0 )
	{		
		$q = "update $tb set ";
		foreach( $data as $f => $v ){
			if( !is_array($v) )
			{
				if( is_string($v))
					$q .= "$field='$value',";
				else
					$q .= "$field=$value,";
			}
			
		}
		$q = rtrim($q, ',');
		$q .= " where $field=$value";
		
		$result = mysql_query( $query ) or die( 'Query_failed: ' . mysql_error() );
		
		return $result? array("status"=>"OK") : array("status"=>"Error", "Error_ID"=>"Update","Error_Description"=>"mysql_error");
	}
	else{
		$d = $data[$tb.'s'];
		
		$q = "update $tb set ";
		foreach( $d as $f => $v ){
			if( !is_array($v) )
			{
				if( is_string($v))
					$q .= "$field='$value',";
				else
					$q .= "$field=$value,";
			}
		}
		$q = rtrim($q, ',');
		$q .= " where $field=$value";
		
		$result = mysql_query( $query ) or die( 'Query_failed: ' . mysql_error() );
		
		if( $result )
		{
			$q = "select id from $tb where $field=$value";
			$r = mysql_query( $query ) or die( 'Query_failed: ' . mysql_error() );
			while( $row = mysql_fetch_row() )
			{
				updateTables($tbs, $tb.'id', $row[0], $d );
			}
			
		}
	}		
}


/* insert into table recursively */
function insertTables( $tbs, $field, $value, $data )
{
	if( sizeof($tbs)==0 )
	{
		return TRUE;
	}
	
	$tb = array_pop( $tbs );
	$dt = $data[$tb.'s'];
	
	foreach( $dt as $d )
	{
		$ks = "insert into $tb (";
		$vs ="values(";
		$ntb = array();
		foreach( $d as $f => $v ){
			if( !is_array($v) )
			{
				$ks .= $f . ',';
				if( is_string($v))
					$vs .= "'$v',";
				else
					$vs .= "$v,";
			}
			else
				$ntb = $d;
		}		
		$ks .= $field . ')';
		$vs .= $value . ')';
		
		$q_ins = $ks . $vs;
		
		//echo( $q_ins );
			//$json_array = array("query"=>"$query");
			//return $json_array;
		$result = mysql_query( $q_ins ) or die( 'Query_failed: ' . mysql_error() );
		
		if( !empty( $ntb ) )
		{
			$res = mysql_query("select last_insert_id()");
			$row = mysql_fetch_row( $res );
			$id = $row[0];
				
			insertTables($tbs, $tb.'id', $id, $ntb );
		}
	}
}


function flatten( $arr )
{
	
	foreach ($arr as $key => $value)
		$str.=$key.':'.$value.';';
    return $str;
       
}

?>